public class Solution {
    public String gcdOfStrings (String str1, String str2) {
        int a = str1.length();
        int b = str2.length();
        for (int i = Math.min(a, b); i > 0; i--) {
            if (a % i == 0 && b % i == 0) {
                String x = str1.substring(0, i);
                if (check(x,str1) && check(x,str2)){
                    return x;
                }
            }

        }
        return "";
    }
    public Boolean check(String a,String b){
        int x = b.length() / a.length();
        StringBuffer ans = new StringBuffer();
        for (int i = 1; i <= x; i++) {
            ans.append(a);
        }
        return ans.toString().equals(b);
    }
}